package org.jeecg.modules.basic.print.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.basic.print.entity.*;
import org.jeecg.modules.basic.print.mapper.PrintConfigMapper;
import org.jeecg.modules.basic.print.mapper.WmsDocumentTypeFieldsMapper;
import org.jeecg.modules.basic.print.mapper.WmsDocumentTypeStatusMapper;
import org.jeecg.modules.basic.print.mapper.WmsMaterialFieldsMapper;
import org.jeecg.modules.basic.print.service.IPrintConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 打印配置
 * @Author: jeecg-boot
 * @Date: 2023-09-13
 * @Version: V1.0
 */
@Service
public class PrintConfigServiceImpl extends ServiceImpl<PrintConfigMapper, PrintConfig> implements IPrintConfigService {

    @Autowired
    private PrintConfigMapper printConfigMapper;
    @Autowired
    private WmsDocumentTypeFieldsMapper wmsDocumentTypeFieldsMapper;
    @Autowired
    private WmsDocumentTypeStatusMapper wmsDocumentTypeStatusMapper;
    @Autowired
    private WmsMaterialFieldsMapper wmsMaterialFieldsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(PrintConfig printConfig, List<WmsDocumentTypeFields> wmsDocumentTypeFieldsList, List<WmsDocumentTypeStatus> wmsDocumentTypeStatusList, List<WmsMaterialFields> wmsMaterialFieldsList) {
        LambdaQueryWrapper<PrintConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PrintConfig::getBusinessType, printConfig.getBusinessType())
                .eq(PrintConfig::getDocumentType, printConfig.getDocumentType());
        Long count = printConfigMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new JeecgBootException("相同单据类型只能存在一条！");
        }
        printConfigMapper.insert(printConfig);
        if (wmsDocumentTypeFieldsList != null && wmsDocumentTypeFieldsList.size() > 0) {
            for (WmsDocumentTypeFields entity : wmsDocumentTypeFieldsList) {
                //外键设置
                entity.setPrintConfigId(printConfig.getId());
                wmsDocumentTypeFieldsMapper.insert(entity);
            }
        }
        if (wmsDocumentTypeStatusList != null && wmsDocumentTypeStatusList.size() > 0) {
            for (WmsDocumentTypeStatus entity : wmsDocumentTypeStatusList) {
                //外键设置
                entity.setPrintConfigId(printConfig.getId());
                wmsDocumentTypeStatusMapper.insert(entity);
            }
        }
        if (wmsMaterialFieldsList != null && wmsMaterialFieldsList.size() > 0) {
            for (WmsMaterialFields entity : wmsMaterialFieldsList) {
                //外键设置
                entity.setPrintConfigId(printConfig.getId());
                wmsMaterialFieldsMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(PrintConfig printConfig, List<WmsDocumentTypeFields> wmsDocumentTypeFieldsList, List<WmsDocumentTypeStatus> wmsDocumentTypeStatusList, List<WmsMaterialFields> wmsMaterialFieldsList) {
        LambdaQueryWrapper<PrintConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PrintConfig::getBusinessType, printConfig.getBusinessType())
                .eq(PrintConfig::getDocumentType, printConfig.getDocumentType());
        List<PrintConfig> configs = printConfigMapper.selectList(queryWrapper);
        if (configs != null && configs.size() > 0 && !configs.get(0).getId().equals(printConfig.getId())) {
            throw new JeecgBootException("业务单据和业务类型不可重复！");
        }
        printConfigMapper.updateById(printConfig);

        //1.先删除子表数据
        wmsDocumentTypeFieldsMapper.deleteByMainId(printConfig.getId());
        wmsDocumentTypeStatusMapper.deleteByMainId(printConfig.getId());
        wmsMaterialFieldsMapper.deleteByMainId(printConfig.getId());

        //2.子表数据重新插入
        if (wmsDocumentTypeFieldsList != null && wmsDocumentTypeFieldsList.size() > 0) {
            for (WmsDocumentTypeFields entity : wmsDocumentTypeFieldsList) {
                //外键设置
                entity.setPrintConfigId(printConfig.getId());
                wmsDocumentTypeFieldsMapper.insert(entity);
            }
        }
        if (wmsDocumentTypeStatusList != null && wmsDocumentTypeStatusList.size() > 0) {
            for (WmsDocumentTypeStatus entity : wmsDocumentTypeStatusList) {
                //外键设置
                entity.setPrintConfigId(printConfig.getId());
                wmsDocumentTypeStatusMapper.insert(entity);
            }
        }
        if (wmsMaterialFieldsList != null && wmsMaterialFieldsList.size() > 0) {
            for (WmsMaterialFields entity : wmsMaterialFieldsList) {
                //外键设置
                entity.setPrintConfigId(printConfig.getId());
                wmsMaterialFieldsMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMain(String id) {
        wmsDocumentTypeFieldsMapper.deleteByMainId(id);
        wmsDocumentTypeStatusMapper.deleteByMainId(id);
        printConfigMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            wmsDocumentTypeFieldsMapper.deleteByMainId(id.toString());
            wmsDocumentTypeStatusMapper.deleteByMainId(id.toString());
            printConfigMapper.deleteById(id);
        }
    }

    @Override
    public List<PrintShowDTO> getPrintShowInfo() {
        List<PrintShowDTO> list = new ArrayList<>();
        List<PrintConfig> printConfigs = printConfigMapper.selectList(null);
        printConfigs.forEach(t -> {
            PrintShowDTO printShowDTO = new PrintShowDTO();
            printShowDTO.setBusinessType(t.getBusinessType());
            printShowDTO.setDocumentType(t.getDocumentType());
            if (StringUtils.isBlank(t.getTemplateUrl())) {
                printShowDTO.setHasShow(0);
            } else {
                printShowDTO.setHasShow(1);
                printShowDTO.setTemplateUrl(t.getTemplateUrl());
            }
            list.add(printShowDTO);
        });

        return list;
    }

}
